//
//  ADUserAcount.swift
//  myBlog
//
//  Created by mac2014 on 16/3/23.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit

/// 通过归档解档来实现账号密码的保存
class ADUserAcount: NSObject {

    /// 用于调用access_token，接口获取授权后的access token
    var access_token: String?

    var expires_in: NSTimeInterval = 0 {
        didSet {
            // 当kKVC给expires_in赋值完成后将expires_in转成NSDate
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    //过期时间
    var expiresDate: NSDate?
    
    /// 当前授权用户的UID
    var uid: String?
    
    //用户名
    var screen_name: String?
    
    //用户头像
    var avatar_large: String?
    
    /// 使用KVC字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        
        // 使用KVC赋值
        setValuesForKeysWithDictionary(dict)
    }
    
    /// 字典中的key在模型中找不到对应的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    // MARK: - 归档和解档
    //将字典转成模型对象，以便写到文件中
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
    }
    
    //返回一个对象
    required init(coder aDecoder: NSCoder) {
        super.init()
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
    }
}

